package net.FamilyTree.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.FamilyTree.entity.People;
import net.FamilyTree.form.SearchForm;
import net.FamilyTree.mapper.PeopleMapper;
import net.FamilyTree.service.PeopleService;
import net.FamilyTree.vo.PageVo;
import net.FamilyTree.vo.PeopleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PeopleServiceImpl extends ServiceImpl<PeopleMapper, People> implements PeopleService {

    @Autowired
    private PeopleMapper peopleMapper;

    /**
     * 添加人物
     * @param people 人物对象
     * @return 添加是否成功的布尔值
     */
    @Override
    public Boolean add(People people) {
        people.setUpdatetime(new Date());
        people.setUserid(1);
        people.setDelflag(0);

        int insert = this.peopleMapper.insert(people);
        // 若insert不等于1，即表示添加失败
        if (insert != 1) {
            return false;
        }
        return true;
    }

    /**
     * 获取人物列表
     * @param page 页码
     * @param size 每页大小
     * @return 包含人物列表和总记录数的PageVo对象
     */
    @Override
    public PageVo list(Integer page, Integer size) {
        Page<People> genealogyPage = new Page<>(page, size);
        Page<People> selectPage = this.peopleMapper.selectPage(genealogyPage, null);
        List<People> peopleList = selectPage.getRecords();
        List<PeopleVo> peopleVoList = new ArrayList<>();
        // 因为实体类People不满足前端显示，所以要将People类转成PeopleVo类
        for (People people : peopleList) {
            PeopleVo peopleVo = new PeopleVo();
            // 将People的数据复制到PeopleVo
            BeanUtils.copyProperties(people, peopleVo);
            peopleVoList.add(peopleVo);
        }
        PageVo pageVo = new PageVo();
        pageVo.setData(peopleVoList);
        pageVo.setTotal(selectPage.getTotal());
        return pageVo;
    }

    /**
     * 搜索人物
     * @param searchForm 搜索表单数据
     * @return 包含搜索结果和总记录数的PageVo对象
     */
    @Override
    public PageVo search(SearchForm searchForm) {
        Page<People> healthPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<People> resultPage;
        // 判断“请输入关键字的框框”中有没有选择值，若有查询与值对应的信息，若没有则查询与当前选择了的字段所有对应的信息
        if (searchForm.getValue().equals("")) {
            resultPage = this.peopleMapper.selectPage(healthPage, null);
        } else {
            QueryWrapper<People> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.peopleMapper.selectPage(healthPage, queryWrapper);
        }
        List<People> peopleList = resultPage.getRecords();
        List<PeopleVo> peopleVoList = new ArrayList<>();
        for (People student : peopleList) {
            PeopleVo peopleVo = new PeopleVo();
            BeanUtils.copyProperties(student, peopleVo);
            peopleVoList.add(peopleVo);
        }
        PageVo pageVo = new PageVo();
        pageVo.setData(peopleVoList);
        pageVo.setTotal(resultPage.getTotal());
        return pageVo;
    }

    /**
     * 更新人物
     * @param people 人物对象
     * @return 更新是否成功的布尔值
     */
    @Override
    public Boolean update(People people) {
        people.setUpdatetime(new Date());
        people.setDelflag(0);
        int update = this.peopleMapper.updateById(people);
        // 若update不等于1，即表示更新失败
        if (update != 1) {
            return false;
        }
        return true;
    }

    /**
     * 删除人物
     * @param id 人物ID
     * @return 删除是否成功的布尔值
     */
    @Override
    public Boolean delete(Integer id) {
        int deleteById = this.peopleMapper.deleteById(id);
        // 若deleteById不等于1，即表示删除失败
        if (deleteById != 1) {
            return false;
        }
        return true;
    }
}
